import { IsItCertified } from '@/enum/user'
import { tip } from '@/utils'

export const navigateTo = (url:string) => {
	if(isLogin()) {
		if(isCertify()) {
			uni.navigateTo({url:url})
		}
	} else {
		uni.reLaunch({url:'/pages/auth/login'})
	}
}

export const switchTab = (url:string) => {
	if(isLogin()) {
		if(isCertify()) {
			uni.switchTab({url:url})
		}
	} else {
		uni.reLaunch({url:'/pages/auth/login'})
	}
}

export const redirectTo = (url:string) => {
	if(isLogin()) {
		if(isCertify()) {
			uni.redirectTo({url:url})
		}
	} else {
		uni.reLaunch({url:'/pages/auth/login'})
	}
}

export const reLaunch = (url:string) => {
	if(isLogin()) {
		if(isCertify()) {
			uni.reLaunch({url:url})
		}
	} else {
		uni.reLaunch({url:'/pages/auth/login'})
	}
}

export const allNavigateTo = (url:string) => {
	if(url === '/pages/index/index' 
	|| url === '/pages/shop/shopList'
	|| url === '/pages/shop/shoppingCart'
	|| url === '/pages/userCenter/index'
	) {
		uni.switchTab({
			url:url
		})
	} else {
		uni.navigateTo({
			url:url
		})
	}
}

export const isNavNormal = () => {
	if(isLogin()) {
		if(isCertify()) {
			return true
		}
		return false
	} else {
		uni.reLaunch({url:'/pages/auth/login'})
		return false
	}
}

export const hasAppRole = () => {
	const appRole = uni.getStorageSync('roles') || []
	if(appRole.includes('APP')) {
		return true
	} else {
		tip.showToast('暂无操作权限，请联系管理员！')
		return false
	} 
}

//是否登录
export const isLogin = () => {
	const token = uni.getStorageSync('token')
	if(token) return true
	return false
}

//去认证
export const goCertify = () => {
	const isItCertified = uni.getStorageSync('isItCertified')
	if([IsItCertified.noSubmit,IsItCertified.noBind].includes(isItCertified)) {
		uni.navigateTo({
			url:'/pages/auth/certify'
		})
	} else {
		uni.navigateTo({
			url:'/pages/auth/certifyDetail'
		})
	}
}

//认证弹框
export const certifyModal = () => {
	uni.showModal({
		title:'温馨提示',
		content:'请先完成企业认证！',
		confirmColor:'#1769ff',
		confirmText:'马上认证',
		cancelText:'返回',
		success:(res:any) => {
			if(res.confirm) {
				goCertify()
			}
		},
	})
}

//是否认证
export const isCertify = () => {
	const isItCertified = uni.getStorageSync('isItCertified')
	if([IsItCertified.noSubmit,IsItCertified.waitAudit,IsItCertified.reject,IsItCertified.noBind].includes(isItCertified)) {
		// tip.showToast('请先认证,认证完后才能体验完整功能！')
		certifyModal()
		return false
	}
	return true
}